/**
 * Created by 刘玉波 on 2016/1/19.
 */
define(['angular', 'app.filters'], function (angular, filters) {
    'use strict';

    //取消转义html，
    // 其中$sce是angularJS自带的安全处理模块，$sce.trustAsHtml(input)方法便是将数据内容以html的形式进行解析并返回。
    // 将此过滤器添加到ng-bind-html所绑定的数据中，便实现了在数据加载时对于html标签的自动转义。
    //例如:<div class="contentwp" ng-bind-html="myArticle.content | trustHtml">
    filters.filter('trustHtml', function ($sce) {
        return function (input) {
            //console.log($sce);
            return $sce.trustAsHtml(input);
        }
    });
    filters.filter('dateStr', function ($sce) {
        return function (input,param) {
            var length=param.length;
            if(input==null){
                return "";
            }else{
                var str=input.substr(0,length);
                return str;
            }
            //var args = Array.prototype.slice.call(arguments);
        }
    });
    filters.filter('subStr', function ($sce) {
        return function (input,length) {
            if(input==null){
                return "";
            }else if(input.length<=length){
                return input;
            }else{
                var str=input.substr(0,length);
                return str+"...";
            }
            //var args = Array.prototype.slice.call(arguments);
        }
    });
    filters.filter('dateRound', function ($sce) {
        return function (input,param) {
            var strs=input.split(".");
            return strs[0];
            //var args = Array.prototype.slice.call(arguments);
        }
    });
    filters.filter('scoreStr', function ($sce) {
        return function (input) {
          if(input>100){
              return 100;
          }else if(input<0) {
              return 0;
          }else{
              return input;
          }
        }
    });
    filters.filter('trustAsResourceUrl', function ($sce) {
        return function(val) {
            return $sce.trustAsResourceUrl(val);
        };
    });
    filters.filter('propsFilter', function() {
        return function(items, props) {
            var out = [];

            if (angular.isArray(items)) {
                items.forEach(function(item) {
                    var itemMatches = false;

                    var keys = Object.keys(props);
                    for (var i = 0; i < keys.length; i++) {
                        var prop = keys[i];
                        var text = props[prop].toLowerCase();
                        if ((item[prop] || '').toString().toLowerCase().indexOf(text) !== -1) {  //曲艺fix：item[prop有可能为空]
                            itemMatches = true;
                            break;
                        }
                    }

                    if (itemMatches) {
                        out.push(item);
                    }
                });
            } else {
                // Let the output be the input untouched
                out = items;
            }

            return out;
        }
    });
    filters.filter('highlight', function() {
        function escapeRegexp(queryToEscape) {
            return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
        }

        return function(matchItem, query) {
            console.log(matchItem);
            return query && matchItem ? matchItem.replace(new RegExp(escapeRegexp(query), 'gi'), '<span class="ui-select-highlight">$&</span>') : matchItem;
        };
    });
});